/*
  学习目标：Redux创建store-推荐写法
*/
import { legacy_createStore as createStore } from 'redux';

// 2. 使用单独的常量,维护state的初始值
const initState = {
  count: 0,
  msg: 'hello React',
  list: [],
  person: { name: 'zs' },
};

// 1. 使用单独的函数, 维护reducer函数
function countReducer(state = initState, action) {
  // 3. 使用switch语句替换if 语句
  switch (action.type) {
    case 'add':
      return { ...state, count: state.count + 1 };
    case 'des':
      return { ...state, count: state.count - 1 };
    default:
      return state;
  }
}

const store = createStore(countReducer);

store.subscribe(() => {
  console.log('store.getState()  ----->  ', store.getState().count);
});

store.dispatch({ type: 'add' });
store.dispatch({ type: 'add' });
store.dispatch({ type: 'add' });
